import {
  Controller,
  Get,
  Post,
  Body,
  UseGuards,
  Query,
  ParseIntPipe,
  Param,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { UserMembershipService } from './user-membership.service';
import { PurchaseMembershipDto } from './dto/purchase-membership.dto';
import { UserMembership } from './entities/user-membership.entity';
import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard';
import { User } from '../../../common/decorators/user.decorator';
import { User as UserEntity } from '../../users/entities/user.entity';

/**
 * 用户会员控制器
 */
@ApiTags('用户会员')
@Controller('member/membership')
@UseGuards(JwtAuthGuard)
export class UserMembershipController {
  constructor(private readonly userMembershipService: UserMembershipService) {}

  @ApiOperation({ summary: '购买会员' })
  @ApiResponse({ status: 201, description: '购买成功', type: UserMembership })
  @Post('purchase')
  async purchaseMembership(
    @User() user: UserEntity,
    @Body() purchaseDto: PurchaseMembershipDto,
  ): Promise<UserMembership> {
    return await this.userMembershipService.purchaseMembership(user.id, purchaseDto);
  }

  @ApiOperation({ summary: '获取当前用户会员信息' })
  @ApiResponse({ status: 200, description: '获取成功', type: UserMembership })
  @Get('current')
  async getCurrentMembership(@User() user: UserEntity): Promise<UserMembership | null> {
    return await this.userMembershipService.getCurrentMembership(user.id);
  }

  @ApiOperation({ summary: '检查用户是否为会员' })
  @ApiResponse({ status: 200, description: '检查成功' })
  @Get('check')
  async checkMembership(@User() user: UserEntity): Promise<{ isMember: boolean }> {
    const isMember = await this.userMembershipService.isUserMember(user.id);
    return { isMember };
  }

  @ApiOperation({ summary: '获取用户会员历史记录' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量', example: 10 })
  @Get('history')
  async getMembershipHistory(
    @User() user: UserEntity,
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('limit', new ParseIntPipe({ optional: true })) limit = 10,
  ): Promise<{
    data: UserMembership[];
    total: number;
    page: number;
    limit: number;
  }> {
    return await this.userMembershipService.getUserMembershipHistory(user.id, page, limit);
  }

  @ApiOperation({ summary: '管理员获取所有用户会员状态' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量', example: 10 })
  @Get('admin/list')
  async getAllUserMemberships(
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('limit', new ParseIntPipe({ optional: true })) limit = 10,
  ): Promise<{
    data: UserMembership[];
    total: number;
    page: number;
    limit: number;
  }> {
    return await this.userMembershipService.getAllUserMemberships(page, limit);
  }

  @ApiOperation({ summary: '管理员手动设置用户会员' })
  @ApiResponse({ status: 201, description: '设置成功', type: UserMembership })
  @Post('admin/set/:userId/:packageId/:durationDays')
  async setUserMembership(
    @Param('userId', ParseIntPipe) userId: number,
    @Param('packageId', ParseIntPipe) packageId: number,
    @Param('durationDays', ParseIntPipe) durationDays: number,
  ): Promise<UserMembership> {
    return await this.userMembershipService.setUserMembership(userId, packageId, durationDays);
  }
}